//detail.js
//获取应用实例
const app = getApp();
const cloudApi = require('../../utils/cloudApi.js');
const img = require('../../utils/img.js');

Page({
  data: {
    marginTop: 0,
    itemInfo: {}, //商品信息json数据
    image: img.image,
    slip: img.slip,
    tkl: '',
    showLoading: true,
    isShare: 1, //判断当前页面是首页进入还是分享进入
    jsonDetail: {
      "vekey": app.globalData.app_key_wytk,
      "para": '579287678299'
    },
    toastJson: {
      showToast: false,
      toastText: '', //轻提示文案
      toastIcon: ''
    },
    showDialog: {
      title: '',
      image: '',
      para: ''
    }
  },
  onShow: function() {
    //后台重新进入小程序，检测剪贴板
    this.getClipboard();
  },
  onLoad: function(option) {
    //获取头部margin
    this.setMarginTop();
    //  获取路由传过来的参数
    this.data.jsonDetail = {
      "vekey": app.globalData.app_key_wytk,
      "para": option.itemId
    };
    this.data.isShare = option.isShare;
    console.log("goods_id = " + this.data.jsonDetail.para);
    this.getItemInfo();
  },
  onShareAppMessage(res) {
    //注册转发小程序功能
    return {
      title: this.data.itemInfo.title,
      path: '/pages/detail/detail?isShare=1&itemId=' + this.data.jsonDetail.para,
      imageUrl: this.data.itemInfo.pict_url
    }
  },
  onPullDownRefresh() {
    //停止下滑动画
    wx.stopPullDownRefresh();
    //获取商品详情
    this.getItemInfo();
  },
  getItemInfo() {
    this.setData({
      showLoading: true
    })
    wx.request({
      url: cloudApi.getAction('detail', this.data.jsonDetail), //请求接口的url
      method: 'GET', //请求方式
      header: {
        'content-type': 'application/json' // 默认值
      },
      complete() { //请求结束后隐藏 loading 提示框
        wx.hideLoading();
      },
      success: res => {
        let result = JSON.parse(res.data.Response.Result.RetMsg);
        this.setData({
          showLoading: false,
          itemInfo: result.data
        });
      }
    })
  },
  copyTKL(event) {
    var that = this;
    var copytxt = event.currentTarget.dataset.tkl;
    //  复制TKL
    wx.setClipboardData({
      data: copytxt,
      success: function(res) {
        wx.hideToast();
        that.showMessage('商品信息复制成功', 'check');
      }
    })
  },
  clickBack() {
    if (this.data.isShare == 0) {
      wx.navigateBack({
        delta: 1
      });
    } else {
      wx.navigateTo({
        url: '/pages/index/index'
      });
    }
  },
  setMarginTop() {
    // 动态计算标题高度
    wx.getSystemInfo({
      success: ({
        statusBarHeight,
        system
      }) => {
        let titleBarHeight = 44
        if (system.indexOf('Android') !== -1) {
          titleBarHeight = 54
        }
        this.setData({
          'marginTop': titleBarHeight + statusBarHeight
        })
      }
    })
  },
  showBigImg(event) {
    wx.previewImage({
      current: event.currentTarget.dataset.src, // 当前显示图片的http链接
      urls: this.data.itemInfo.small_images // 需要预览的图片http链接列表
    })
  },
  collect() {
    this.showMessage('收藏功能开发中。。。', 'warning');
  },
  showMessage(toastText, toastIcon) {
    this.setData({
      toastJson: {
        toastText: toastText,
        toastIcon: toastIcon,
        showToast: true
      }
    });
    setTimeout(() => {
      this.setData({
        toastJson: {
          showToast: false
        }
      })
    }, 1000);
  },
  getClipboard(callback) {
    var that = this;
    wx.getClipboardData({
      success: function(res) {
        if (res.data.indexOf("€") != -1 || res.data.indexOf("￥") != -1) {
          var jsonDetail = {
            "vekey": app.globalData.app_key_wytk,
            "para": res.data
          };
          wx.request({
            url: cloudApi.getAction('detail', jsonDetail), //请求接口的url
            method: 'GET', //请求方式
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: res => {
              that.setData({
                showDialog: {
                  title: JSON.parse(res.data.Response.Result.RetMsg).data.title,
                  image: JSON.parse(res.data.Response.Result.RetMsg).data.pict_url,
                  para: JSON.parse(res.data.Response.Result.RetMsg).data.num_iid
                }
              });
              //清空剪贴板
              wx.setClipboardData({
                data: '.',  //手机下不能设置为空字符串
                success(res) {
                  wx.hideToast();
                }
              });
              that.showDialog();
            }
          })
        }
      },
    });
  },
  showDialog() {
    let dialogComponent = this.selectComponent('.wxc-dialog')
    dialogComponent && dialogComponent.show();
  },
  hideDialog() {
    let dialogComponent = this.selectComponent('.wxc-dialog')
    dialogComponent && dialogComponent.hide();
  },
  onConfirm() {
    this.hideDialog();
    var that = this;
    //跳转详情页面
    wx.navigateTo({
      url: '/pages/detail/detail?isShare=0&itemId=' + that.data.showDialog.para
    });
  },
  onCancel() {
    this.hideDialog();
  }
})